<html>
<!--

DENWER
Copyright (C) 2001-2005 Dmitry Koteroff

DENWER Link Creator
Copyright (C) 2005,2008 Ildar Shaimordanov

-->
<head>
<HTA:APPLICATION ID="oHTA"
  APPLICATIONNAME="DENWERLink"
  BORDER="dialog"
  BORDERSTYLE="static"
  CAPTION="yes"
  CONTEXTMENU="no"
  ICON=""
  INNERBORDER="no"
  MAXIMIZEBUTTON="no"
  MINIMIZEBUTTON="no"
  NAVIGABLE="no"
  SCROLL="no"
  SCROLLFLAT="no"
  SELECTION="no"
  SHOWINTASKBAR="yes"
  SINGLEINSTANCE="yes"
  SYSMENU="yes"
  VERSION="1.0"
  WINDOWSTATE="normal"/>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1251" />
<title>DENWER Link</title>
<style type="text/css">

*	{
	font-family: Verdana, Helvetica, Tahoma, 'Courier New', sans-serif;
}

body	{
	background-color: #CCCCCC;
	font-family: Verdana, Helvetica, Tahoma, 'Courier New', sans-serif;
	margin: 10px;
	padding: 0;
	text-align: left;
}

table	{
	width: 100%;
}

.label	{
	text-align: right;
	vertical-align: top;
	width: 150px;
}

select	{
	width: 100%;
}

.button	{
	width: 100px;
}

</style>
<script type="text/javascript" language="javascript">

// System dependent handlers
var fso = new ActiveXObject('Scripting.FileSystemObject');
var wsh = new ActiveXObject('WScript.Shell');

// DENWER installed folders
var denwerCtrlPath2 = 'etc';
var denwerCtrlPath3 = 'denwer';
var denwerStdFolders = [
	'(' + denwerCtrlPath2 + '|' + denwerCtrlPath3 + ')', 
	'home', 
	'tmp', 
	'usr'
];
var denwerStdFoldersRegex = new RegExp('\\b(?:' + denwerStdFolders.join('|') + ')\\b', 'g');

// DENWER control applications
var shortcuts = [
	{
		name: 'Start servers', 
		prog: 'Run.exe', 
		wstyle: 1, 
		hotkey: '', 
		icon: 'run.ico', 
		descr: '', 
		wdir: ''
	},
	{
		name: 'Stop servers', 
		prog: 'Stop.exe', 
		wstyle: 1, 
		hotkey: '', 
		icon: 'stop.ico', 
		descr: '', 
		wdir: ''
	},
	{
		name: 'Restart servers', 
		prog: 'Restart.exe', 
		wstyle: 1, 
		hotkey: '', 
		icon: 'restart.ico', 
		descr: '', 
		wdir: ''
	}
];

// Lookup DENWER installation
function getDenwerFolder(spec)
{
	var f = fso.GetFolder(spec);
	var fc = new Enumerator(f.SubFolders);

	var result = [];
	for ( ; ! fc.atEnd(); fc.moveNext()) {
		var folder = fc.item();
		result[result.length] = folder.Name;
	}

	// amount of folders at least the same as amount of the DENWER folders
	if ( denwerStdFolders.length > result.length ) {
		return false;
	}

	// contains the same folders like the DENWER
	result = result.sort().join(':').match(denwerStdFoldersRegex);
	if ( ! result || result.length != denwerStdFolders.length ) {
		return false;
	}

	// Control path /etc for DENWER-2 and /denwer for DENWER-3
	var denwerCtrlPath = result[0];

	// contains control files exactly
	for (var i = 0; i < shortcuts.length; i++) {
		if ( ! fso.FileExists(spec + '\\' + denwerCtrlPath + '\\' + shortcuts[i].prog) ) {
			return false;
		}
		if ( ! fso.FileExists(spec + '\\' + denwerCtrlPath + '\\utils\\' + shortcuts[i].icon) ) {
			return false;
		}
	}

	return spec;
};

function getDenwerList()
{
	var result = [];

	var e = new Enumerator(fso.Drives);
	for ( ; ! e.atEnd(); e.moveNext()) {
		var drive = e.item();

		// skip network drives
		if ( drive.DriveType == 3 ) {
			continue;
		}

		// ready drives only
		if ( ! drive.IsReady ) {
			continue;
		}

		// specific folders only
		var path = getDenwerFolder(drive.RootFolder);
		if ( path ) {
			// this the DENWER drive
			result[result.length] = path;
		} else {
			// lookup for DENWER folder
			var f = fso.GetFolder(drive.RootFolder);
			var fc = new Enumerator(f.SubFolders);

			for ( ; ! fc.atEnd(); fc.moveNext()) {
				var folder = fc.item();
				path = getDenwerFolder(folder);
				if ( path ) {
					result[result.length] = path;
				}
			}
		}
	}

	return result;
};

function createDenwerList()
{
	var list = getDenwerList();

	if ( list && list.length ) {
		document.getElementById('execute').disabled = false;
	}

	var installPath = document.getElementById('installpath');

	for (var i = 0; i < list.length; i++) {
		installPath.options[i] = new Option(list[i], list[i]);
	}
};


// Creator of shortcuts
function createShortcut(location, installPath, shortcut)
{
	// workaround to differ DENWER-2 and DENWER-3 when creating shortcuts
	if ( fso.FolderExists(installPath + '\\' + denwerCtrlPath3) ) {
		var denwerCtrlPath = denwerCtrlPath3;
		var shortcutName = shortcut.name.replace(/servers/, 'Denwer');
	} else {
		var denwerCtrlPath = denwerCtrlPath2;
		var shortcutName = shortcut.name;
	}

	var lnk = wsh.CreateShortcut(location + '\\' + shortcutName + '.lnk');
	lnk.TargetPath       = installPath + '\\' + denwerCtrlPath + '\\' + shortcut.prog;
	lnk.WindowStyle      = shortcut.wstyle;
	lnk.HotKey           = shortcut.hotkey;
	lnk.IconLocation     = installPath + '\\' + denwerCtrlPath + '\\utils\\' + shortcut.icon;
	lnk.Description      = shortcut.descr;
	lnk.WorkingDirectory = (shortcut.wdir) ? shortcut.wdir : installPath + '\\' + denwerCtrlPath;
	lnk.Save();
};

function execCreateShortcut()
{
	var allUsers, location, installPath;

	allUsers = document.getElementById('allusers').checked;

	location = document.getElementById('location').options;
	location = location.options[location.selectedIndex].value;
	if ( allUsers ) {
		location = 'AllUsers' + location;
	}
	location = wsh.SpecialFolders(location);

	installPath = document.getElementById('installpath').options;
	installPath = installPath.options[installPath.selectedIndex].value;

	if ( location.match(/Startup/) ) {
		createShortcut(location, installPath, shortcuts[0]);
	} else {
		for (var i = 0; i < shortcuts.length; i++) {
			createShortcut(location, installPath, shortcuts[i]);
		}
	}

	if (confirm('All links are created.\nClose the application?')) {
		window.close();
	}
};

</script>
<script type="text/javascript" language="javascript">

// Initialization of window
function initWin()
{
	if (oHTA.applicationName !== "DENWERLink") {
		window.close();
		return;
	}

	// sets window size
	with (window.screen) {
		var w = 600, h = 400;
		moveTo((width - w) / 2, (height - h) / 2);
		resizeTo(w, h);
	}

	createDenwerList();
};

</script>
</head>

<body onload="initWin()">

<p>
<h4>DENWER Link Version 3</h4>
Utility for creating of links for the DENWER
</p>

<form>
<table>
<tr>
<td class="label">

<label for="installpath">DENWER folder</label>

</td>
<td>

<select id="installpath" size="5">
<option></option>
</select>

</td>
</tr>
<tr>
<td class="label">

<label for="location">Location</label>

</td>
<td>

<select id="location" size="3">
<option value="Desktop">Desktop</option>
<option value="StartMenu">Start Menu</option>
<option value="Startup">Startup</option>
</select>

</td>
</tr>
<tr>
<td class="label">&nbsp;</td>
<td>

<input type="checkbox" id="allusers" value="1" /><label for="allusers">For all users</label>

</td>
</tr>
<tr>
<td class="label">&nbsp;</td>
<td>

<input type="button" class="button" id="execute" value="Create" disabled="disabled" onclick="execCreateShortcut()" />
<input type="button" class="button" id="close" value="Close" onclick="window.close()" />

</td>
</tr>
</table>
</form>

</body>
</html>

